﻿// -----------------------------------------------------------------------
// <copyright file="PhieuKhamBenhDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class PhieuKhamBenhDataAccess
    {
        public bool InsertPhieuKhamBenh(PhieuKhamBenhEntity obj)
        {
            try
            {
                string sql = "spPhieuKhamBenh_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhamBenh", DbType.String, obj.MaKhamBenh);
                db.AddInParameter(cmd, "Stt", DbType.Int32, obj.Stt);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, obj.MaHoSo);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, obj.MaDichVu);
                db.AddInParameter(cmd, "SoVaoVien", DbType.String, obj.SoVaoVien);
                db.AddInParameter(cmd, "NgayVaoKham", DbType.DateTime, obj.NgayVaoKham);
                db.AddInParameter(cmd, "LyDoKham", DbType.String, obj.LyDoKham);
                db.AddInParameter(cmd, "KetLuan", DbType.String, obj.KetLuan);
                db.AddInParameter(cmd, "MaBacSy", DbType.String, obj.MaBacSy);
                db.AddInParameter(cmd, "TrangThaiPhieu", DbType.Int32, obj.TrangThaiPhieu);
                db.AddInParameter(cmd, "MaKhoa", DbType.String, obj.MaKhoa);
                db.AddInParameter(cmd, "MaPhongKham", DbType.String, obj.MaPhongKham);
                db.AddInParameter(cmd, "TrangThaiCho", DbType.Boolean, obj.TrangThaiCho);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertPhieuKhamBenh", ex.Message); throw ex; }
        }

        public bool EditPhieuKhamBenh(PhieuKhamBenhEntity obj)
        {
            try
            {
                string sql = "spPhieuKhamBenh_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhamBenh", DbType.String, obj.MaKhamBenh);
                db.AddInParameter(cmd, "Stt", DbType.Int32, obj.Stt);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, obj.MaHoSo);
                db.AddInParameter(cmd, "MaDichVu", DbType.String, obj.MaDichVu);
                db.AddInParameter(cmd, "SoVaoVien", DbType.String, obj.SoVaoVien);
                db.AddInParameter(cmd, "NgayVaoKham", DbType.DateTime, obj.NgayVaoKham);
                db.AddInParameter(cmd, "LyDoKham", DbType.String, obj.LyDoKham);
                db.AddInParameter(cmd, "KetLuan", DbType.String, obj.KetLuan);
                db.AddInParameter(cmd, "MaBacSy", DbType.String, obj.MaBacSy);
                db.AddInParameter(cmd, "TrangThaiPhieu", DbType.Int32, obj.TrangThaiPhieu);
                db.AddInParameter(cmd, "MaKhoa", DbType.String, obj.MaKhoa);
                db.AddInParameter(cmd, "MaPhongKham", DbType.String, obj.MaPhongKham);
                db.AddInParameter(cmd, "TrangThaiCho", DbType.Boolean, obj.TrangThaiCho);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdatePhieuKhamBenh", ex.Message); throw ex; }
        }

        public bool DeletePhieuKhamBenh(string maKhamBenh)
        {
            try
            {
                string sql = "spPhieuKhamBenh_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhamBenh", DbType.String, maKhamBenh);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeletePhieuKhamBenh", ex.Message); throw ex; }
        }

        public PhieuKhamBenhEntity GetPhieuKhamBenh(string maKhamBenh)
        {
            try
            {
                PhieuKhamBenhEntity obj = null;
                string sql = "spPhieuKhamBenh_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhamBenh", DbType.String, maKhamBenh);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuKhamBenhEntity();
                        obj.MaKhamBenh = reader["MaKhamBenh"].ToString();
                        obj.Stt = String.IsNullOrEmpty(reader["Stt"].ToString()) == true ? 0 : (Int32)reader["Stt"];
                        obj.MaHoSo = reader["MaHoSo"].ToString();
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.SoVaoVien = reader["SoVaoVien"].ToString();
                        obj.NgayVaoKham = String.IsNullOrEmpty(reader["NgayVaoKham"].ToString()) == true ? DateTime.MinValue : (DateTime)reader["NgayVaoKham"];
                        obj.LyDoKham = reader["LyDoKham"].ToString();
                        obj.KetLuan = reader["KetLuan"].ToString();
                        obj.MaBacSy = reader["MaBacSy"].ToString();
                        obj.TrangThaiPhieu = String.IsNullOrEmpty(reader["TrangThaiPhieu"].ToString()) == true ? 0 : (Int32)reader["TrangThaiPhieu"];
                        obj.MaKhoa = reader["MaKhoa"].ToString();
                        obj.MaPhongKham = reader["MaPhongKham"].ToString();
                        obj.TrangThaiCho = String.IsNullOrEmpty(reader["TrangThaiCho"].ToString()) == true ? false : (bool)reader["TrangThaiCho"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuKhamBenh", ex.Message); throw ex; }
        }

        public List<PhieuKhamBenhEntity> GetPhieuKhamBenhList(DateTime ngayVaoKham, int trangThaiPhieu)
        {
            try
            {
                List<PhieuKhamBenhEntity> list = new List<PhieuKhamBenhEntity>();
                PhieuKhamBenhEntity obj = null;
                string sql = "spPhieuKhamBenh_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "NgayVaoKham", DbType.DateTime, ngayVaoKham);
                db.AddInParameter(cmd, "TrangThaiPhieu", DbType.Int32, trangThaiPhieu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuKhamBenhEntity();
                        obj.MaKhamBenh = reader["MaKhamBenh"].ToString();
                        obj.Stt = String.IsNullOrEmpty(reader["Stt"].ToString()) == true ? 0 : (Int32)reader["Stt"];
                        obj.MaHoSo = reader["MaHoSo"].ToString();
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.SoVaoVien = reader["SoVaoVien"].ToString();
                        obj.NgayVaoKham = String.IsNullOrEmpty(reader["NgayVaoKham"].ToString()) == true ? DateTime.MinValue : (DateTime)reader["NgayVaoKham"];
                        obj.LyDoKham = reader["LyDoKham"].ToString();
                        obj.KetLuan = reader["KetLuan"].ToString();
                        obj.MaBacSy = reader["MaBacSy"].ToString();
                        obj.TrangThaiPhieu = String.IsNullOrEmpty(reader["TrangThaiPhieu"].ToString()) == true ? 0 : (Int32)reader["TrangThaiPhieu"];
                        obj.MaKhoa = reader["MaKhoa"].ToString();
                        obj.MaPhongKham = reader["MaPhongKham"].ToString();
                        obj.TrangThaiCho = String.IsNullOrEmpty(reader["TrangThaiCho"].ToString()) == true ? false : (bool)reader["TrangThaiCho"];
                        obj.TenPhongKham = reader["TenPhongKham"].ToString();
                        obj.TenKhoa = reader["TenKhoa"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuKhamBenhList", ex.Message); throw ex; }
        }

        public List<PhieuKhamBenhEntity> GetAllPhieuKhamBenhByMaHoSo(string maHoSo)
        {
            try
            {
                List<PhieuKhamBenhEntity> list = new List<PhieuKhamBenhEntity>();
                PhieuKhamBenhEntity obj = null;
                string sql = "sptblHoSoKhamBenh_SelectAllDichVu";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, maHoSo);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuKhamBenhEntity();
                        obj.MaKhamBenh = reader["MaKhamBenh"].ToString();
                        obj.MaHoSo = reader["MaHoSo"].ToString();
                        obj.MaDichVu = reader["MaDichVu"].ToString();
                        obj.NgayVaoKham = String.IsNullOrEmpty(reader["NgayVaoKham"].ToString()) == true ? DateTime.MinValue : (DateTime)reader["NgayVaoKham"];
                        obj.LyDoKham = reader["LyDoKham"].ToString();
                        obj.TrangThaiPhieu = String.IsNullOrEmpty(reader["TrangThaiPhieu"].ToString()) == true ? 0 : (Int32)reader["TrangThaiPhieu"];
                        obj.MaLoaiDichVu = reader["MaLoaiDichVu"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetAllPhieuKhamBenhByMaHoSo", ex.Message); throw ex; }

        }

    }
}